var util = require('./utils/util.js')
var glo = require('./utils/global.js')
var bibleData = require('./utils/bible.js')
var config = require('./utils/config.js')
//app.js
App({
  onLaunch: function() {
    wx.setKeepScreenOn({
      keepScreenOn: true,
    })
    //调用API判断当前网络连接状态，若是wifi下连接则缓存
    wx.getNetworkType({
      success: function(res) {
        var info = wx.getStorageInfoSync();
        if (!util.contains(info.keys, "version")) {
          wx.setStorageSync('version', '-1');
        }
        // 返回网络类型, 有效值：
        // wifi/2g/3g/4g/unknown(Android下不常见的网络类型)/none(无网络)
        var networkType = res.networkType
        wx.setStorageSync('storageing', '0'); //设置缓初始标记
        // if (networkType == "wifi") {           //wifi下直接开始缓存
        if(glo.checkDate()){
          glo.wifiUpdateBibleData(null);
          wx.showLoading({
            title: '加载中···',
            mask: true
          })
        }
        
      
        //  }
      }
    })

   


    var info = wx.getStorageInfoSync();
    for (var i = 0; i < 25; i++) {
      if (!util.contains(info.keys, "lsr" + i)) {
        wx.setStorageSync("lsr" + i, '0');
      }
    }
  },    
  setUserInfo: function(cb) {
    this.globalData.userInfo = cb;
  },
  getUserInfo: function(cb, flag) {
    var that = this;
    if (flag == '1') {
      flag = false;
    } else {
      flag = true;
    }
    if (that.globalData.userInfo && flag) {
      typeof cb == "function" &&
        cb(that.globalData.userInfo)
    } else {
      console.log(flag + '====================');
      wx.getNetworkType({
        success: function(res0) {
          var networkType = res0.networkType;
          if (networkType == "none") {
            that.globalData.userInfo = wx.getStorageSync('userInfo');
            console.log('22:' + that.globalData.userInfo);
            typeof cb == "function" && cb(that.globalData.userInfo);
            return;
          }
          wx.login({
            success: function(res) {
              wx.request({
                url: glo.https + '/person/' + config.register,
                data: {
                  js_code: res.code,
                  avatar: '',
                  city: '',
                  country: '',
                  language: '',
                  nickname: '',
                  province: '',
                  gender: ''
                },
                success: function(res2) {
                    console.log(res2)
                  that.globalData.userInfo = res2.data;
                  wx.setStorageSync('userInfo', that.globalData.userInfo);
                  console.log(that.globalData.userInfo);
                  typeof cb == "function" && cb(that.globalData.userInfo)
                  // wx.setStorageSync('userid', res2.data.userid);
                }
              })
            }
          });

        },
        fail: function(res2) {
          console.log(2222);
          var a = res2;
        }
      })
    }

  },
  globalData: {
    userInfo: null,
    globalUrl: "https://apis.juhe.cn/cook/query.php?key=fe35d033b5f6aed8666163e82b06e246"
  }
})